package com.ruoyi.eam.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.ruoyi.eam.complex.StokByAssetList;
import com.ruoyi.eam.doman.EamAsset;
import com.ruoyi.eam.doman.EamStockD;
import com.ruoyi.eam.service.IEamAssetService;
import com.ruoyi.eam.service.IEamStockDService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.eam.doman.EamStock;
import com.ruoyi.eam.service.IEamStockService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 入库信息Controller
 * 
 * @author ruoyi
 * @date 2024-06-20
 */
@RestController
@RequestMapping("/stock")
public class EamStockController extends BaseController
{
    @Autowired
    private IEamStockService eamStockService;
    @Autowired
    private IEamStockDService eamStockDService;
    @Autowired
    private IEamAssetService eamAssetService;

    /**
     * 查询入库信息列表
     */
    //@RequiresPermissions("system:stock:list")
    @GetMapping("/list")
    public TableDataInfo list(EamStock eamStock)
    {
//        System.out.println(eamStock);
        EamAsset asset = new EamAsset();
        asset.setAssetCode(eamStock.getAssetCode());
        if(eamStock.getAssetCode() != null){
            if(eamAssetService.selectEamAssetList(asset).size() > 0){
                eamStock.setAssetId(eamAssetService.selectEamAssetList(asset).get(0).getAssetId());
            }else{
                eamStock.setAssetId(9999999999L);
            }
        }
        startPage();
        List<EamStock> list = eamStockService.selectEamStockList(eamStock);
        return getDataTable(list);

    }

    /**
     * 导出入库信息列表
     */
//    @RequiresPermissions("system:stock:export")
    @Log(title = "入库信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EamStock eamStock)
    {
        List<EamStock> list = eamStockService.selectEamStockList(eamStock);
        ExcelUtil<EamStock> util = new ExcelUtil<EamStock>(EamStock.class);
        util.exportExcel(response, list, "入库信息数据");
    }

    /**
     * 获取入库信息详细信息
     */
    //@RequiresPermissions("system:stock:query")
    @GetMapping(value = "/{stockId}")
    public AjaxResult getInfo(@PathVariable("stockId") Long stockId)
    {
        return success(eamStockService.selectEamStockByStockId(stockId));
    }

    /**
     * 新增入库信息
     */
    //@RequiresPermissions("system:stock:add")
    @Log(title = "入库信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody StokByAssetList stokByAssetList)
    {
        System.out.println(stokByAssetList.getStock());
        //System.out.println(stokByAssetList);
        if(stokByAssetList.getStock().getStockId() == null){
            return toAjax(eamStockService.insertEamStock(stokByAssetList));
        }else{
            eamStockService.deleteEamStockByStockId(stokByAssetList.getStock().getStockId());
            return toAjax(eamStockService.insertEamStock(stokByAssetList));
        }

    }

    /**
     * 修改入库信息
     */
//    @RequiresPermissions("system:stock:edit")
    @Log(title = "入库信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EamStock eamStock)
    {
        return toAjax(eamStockService.updateEamStock(eamStock));
    }

    /**
     * 删除入库信息
     */
    //@RequiresPermissions("system:stock:remove")
    @Log(title = "入库信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{stockIds}")
    public AjaxResult remove(@PathVariable Long[] stockIds)
    {
        return toAjax(eamStockService.deleteEamStockByStockIds(stockIds));
    }
}
